#include<stdio.h>
#include<conio.h>
typedef struct Room{
    long start,end,index;
    Room *left,*right,*up;
};
long i,n,arr[100066],count=0,id=0;
Room *list=NULL;
void add(long start,long end,long index){
    Room *q=NULL,*p=list;
    while(p!=NULL){
        q=p;
        if(start>=(*p).start)p=(*p).right;
        else p=(*p).left;
    }
    p=new Room;
    (*p).start=start;
    (*p).end=end;
    (*p).index=index;
    (*p).up=(*p).right=(*p).left=NULL;
    if(list==NULL)list=p;
    else{
        if(start<(*q).start)(*q).left=p;
        else (*q).right=p;
        (*p).up=q;
    }
}
void input(){
    long u,v;
    FILE *fp=fopen("ROOMS.INP","r");
    fscanf(fp,"%ld",&n);
    for(i=0;i<n;i++){
        fscanf(fp,"%ld%ld",&u,&v);
        add(u,v,i+1);
    }
    fclose(fp);
}
void out(Room *a){
    if(a!=NULL){
        out((*a).left);
        printf("%ld :: %ld %ld\n",(*a).index,(*a).start,(*a).end);
        out((*a).right);
    }
}
Room *getMin(){
    Room *q=list;
    while((*q).left!=NULL)q=(*q).left;
    return q;
}
/* Tra ve end cua phong duoc xoa*/
long del(long v){
    Room *find=list,*accept=NULL,*befor;
    long start;
    while(find!=NULL){
        start=(*find).start;
        if(start>=v)accept=find;
        if(start==v)break;
        if(start<v)find=(*find).right;
        else find=(*find).left;
    }
    
    if(accept==NULL)return 0;

    arr[++id]=(*accept).index;
    start=(*accept).end;
    /* Neu co 2 nhanh */
    if((*accept).left!=NULL && (*accept).right!=NULL){
        befor=accept;
        find=(*accept).left;
        while((*find).right!=NULL){
            befor=find;
            find=(*find).right;
        }
        (*accept).start=(*find).start;
        (*accept).end=(*find).end;
        (*accept).index=(*find).index;

        if(befor==accept)(*accept).left=(*find).left;
        else (*befor).right=(*find).left;
        delete find;
        return start;
    }
    befor=(*accept).up;
    /* Chi co toi da 1 nhanh*/
    /* Co chan phai right*/
    if((*accept).right!=NULL){
        if(list==accept)list=(*accept).right;
        else{
            if((*befor).left==accept)(*befor).left=(*accept).right;
            else (*befor).right=(*accept).right;
        }
    }else{
        /* Co chan trai left*/
        if(list==accept)list=(*accept).left;
        else{
            if((*befor).left==accept)(*befor).left=(*accept).left;
            else (*befor).right=(*accept).left;
        }
    }
    delete accept;
    return start;
} 
void process(){
    Room *p;
    long k=0;
    arr[0]=-1;
    while(list!=NULL){
        p=getMin();
        k=(*p).start;
        do{
            printf("\n_______________________\nXoa gia tri %ld\n",k);
            k=del(k);
            out(list);
            getch();
        }while(k>0);
        arr[++id]=-1;
        count++;
    };
}
void output(){
    FILE *fp=fopen("ROOMS.OUT","w");
    fprintf(fp,"%ld",count);
    for(long i=0;i<=id;i++)
        if(arr[i]==-1)fprintf(fp,"\n");
        else{
            if(i>0 && arr[i-1]>0)fprintf(fp," ");
            fprintf(fp,"%ld",arr[i]);
        }
    fclose(fp);
}
int main(){
    input();
    out(list);
    process();
    //output();
    //getch();
    out(list);
    getch();
    return 0;
}
    
